﻿using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace Fuse8.DomainFramework.Domain
{
	internal class RelatedPropertyCollection : IEnumerable<RelatedProperty>
	{
		private readonly List<RelatedProperty> _properties = new List<RelatedProperty>();
		
		public void Add (RelatedProperty property)
		{
			var currentProperties = _properties;

			if (!currentProperties.Any(p => p.PropertyName == property.PropertyName))
			{
				_properties.Add(property);
			}
			else
			{
				var currentProperty = _properties.First(p => p.PropertyName == property.PropertyName);
				foreach (RelatedProperty relatedPrroperty in property.RelatedProperties)
				{
					currentProperty.RelatedProperties.Add(relatedPrroperty);
				}
			}
		}

		public void AddCollection (RelatedPropertyCollection collection)
		{
			if (collection == null)
			{
				return;
			}

			foreach (RelatedProperty property in collection)
			{
				Add(property);
			}
		}

		IEnumerator<RelatedProperty> IEnumerable<RelatedProperty>.GetEnumerator()
		{
			return _properties.GetEnumerator();
		}

		public IEnumerator GetEnumerator()
		{
			return _properties.GetEnumerator();
		}
	}
}